#!/bin/bash
# 

if [[ $(id -u) -ne 0 ]]; then
  echo "This script must be executed as root or using sudo."
    exit 99
fi

ARCH=$(dpkg --print-architecture)
dist="$(lsb_release --codename --short)"
SDIST=${dist,,}
CUDIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"

# config system dns 
if [ -f /etc/systemd/resolved.conf ];then
    if ! $(grep -i "^dns"  /etc/systemd/resolved.conf > /dev/null) ;then
        echo "DNS=114.114.114.114 8.8.8.8" >> /etc/systemd/resolved.conf
    else
        sed -i "s/^DNS.*$/DNS=114.114.114.114 8.8.8.8/" /etc/systemd/resolved.conf
    fi
else
    echo -e "nameserver 114.114.114.114\nnameserver 8.8.8.8" > /etc/resolv.conf
fi
systemctl restart systemd-resolved
systemctl enable systemd-resolved --now

plugin_list="
openmediavault-apttool
openmediavault-autoshutdown
openmediavault-backup
openmediavault-borgbackup
openmediavault-usbbackup
openmediavault-clamav
openmediavault-fail2ban
openmediavault-resetperms
openmediavault-diskstats
openmediavault-diskclone
openmediavault-mergerfs
openmediavault-remotemount
openmediavault-rsnapshot
openmediavault-filebrowser
openmediavault-flashmemory
openmediavault-sftp
openmediavault-sharerootfs
openmediavault-symlinks
openmediavault-wakealarm
openmediavault-wol
openmediavault-lvm2
openmediavault-snapraid
openmediavault-s3
openmediavault-tgt
openmediavault-md
openmediavault-mounteditor
openmediavault-podman
openmediavault-snmp
openmediavault-webdav
openmediavault-ftp
openmediavault-hosts
openmediavault-kernel
"

plugin_list_ext="
openmediavault-iperf3
openmediavault-compose
openmediavault-scripts
openmediavault-kvm
openmediavault-zfs
"

cat <<EOF > /etc/apt/sources.list
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ ${SDIST} main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ ${SDIST}-updates main contrib non-free non-free-firmware
EOF


cat <<EOF > ~/.wget-hsts
# HSTS 1.0 Known Hosts database for GNU Wget.
# <hostname>                 <port>  <incl. subdomains>  <created>  <max-age>
mirror.ghproxy.com              0       0       1714826189      63072000
packages.openmediavault.org     0       0       1714822418      31536000
github.com                      0       1       1714826086      31536000
EOF


apt clean && apt update
apt -y install apt-transport-https ca-certificates gnupg gnupg2 software-properties-common sudo vim curl wget
apt -y install debian-keyring debian-archive-keyring
apt-key update 

[ ! -f /etc/docker ]&& mkdir -p  /etc/docker
wait
cat > /etc/docker/daemon.json << 'EOF'
{
    "data-root": "/var/lib/docker",
    "registry-mirrors": [
        "https://registry.docker-cn.com",
        "https://hub-mirror.c.163.com",
        "https://ccr.ccs.tencentyun.com",
        "https://docker.mirrors.ustc.edu.cn"
    ]
}
EOF

bash ${CUDIR}/omv-apt-init

apt-get update
apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin php8.2-libvirt-php
source /etc/profile

wait
wget -O /usr/local/bin/yq  https://mirror.ghproxy.com/https://github.com/mikefarah/yq/releases/download/v4.35.2/yq_linux_"${ARCH}"
wait
wget -O /usr/local/bin/regctl  https://mirror.ghproxy.com/https://github.com/regclient/regclient/releases/download/v0.6.0/regctl-linux-${ARCH}
wait
wget -O /usr/bin/youtube-dl  https://mirror.ghproxy.com/https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp
wait
chmod a+x /usr/local/bin/*  /usr/bin/youtube-dl
wait

apt  -y install  ${plugin_list}
if [ $? -gt 0 ]; then
    echo "Installing other dependencies ..."
    apt-get -f install
    apt  -y install  ${plugin_list}
fi
wait
apt  -y install  ${plugin_list_ext}
if [ $? -gt 0 ]; then
    echo "Installing other dependencies ..."
    apt-get -f install
    apt  -y install  ${plugin_list_ext}
fi
wait
#  install downloader from zh_cn
[ -f ${CUDIR}/debian/deb/openmediavault-downloader-cn_7.0_all.deb ]&& dpkg -i ${CUDIR}/debian/deb/openmediavault-downloader-cn_7.0_all.deb
if [ $? -gt 0 ]; then
    echo "Installing other dependencies ..."
    apt-get -f install
fi

# set timezone Asia/Shanghai
timedatectl set-timezone Asia/Shanghai

systemctl daemon-reload

systemctl restart docker

source  /etc/profile

omv-confdbadm populate

echo -e "\n OMV 7 extras App installation has been completed \n"

exit 0